package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import dao.Products;
import model.*;

public class LoadProducts extends HttpServlet {
private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
											throws ServletException, IOException {
		//PrintWriter out = response.getWriter();
		HttpSession sessionGH = request.getSession();
		HttpSession sessionHoamoi = request.getSession();
		HttpSession tb = request.getSession();
		String action = request.getParameter("action") == null?"":request.getParameter("action");
		String mahoa = request.getParameter("mahoa");
		Products productDTO = new Products();
		List<ProductModel> h = new ArrayList<ProductModel>();
		h = productDTO.getProducts(mahoa, null, null, null);
		if(action.equals("xoa")){
			
			
			ShoppingCart gh = (ShoppingCart)sessionGH.getAttribute("gh");
			if(h.size() > 0){
				gh.delete(h.get(0));
			}
			sessionGH.setAttribute("gh", gh);
			response.sendRedirect("Trang_xem_gh.jsp");		
			
		}
		else {
			if(action.equals("capnhat")){
				if(h.size() >0){
					sessionGH.setAttribute("hoacapnhat", h.get(0));
					response.sendRedirect("Trang_cap_nhat_hoa.jsp");
				}
			}
			else{
				if(action.equals("capnhatlai")){
					String soluong = request.getParameter("txtSoluongcapnhat");
					int sl = Integer.parseInt(soluong);
					
					if(sl<1){
						String tbao1 = "Số lượng không hợp lý";
						tb.setAttribute("tb", tbao1);
						response.sendRedirect("Trang_cap_nhat_hoa.jsp");
					}
					else{
						
						String productId = request.getParameter("txtMahoa");
						h = productDTO.getProducts(productId, null, null, null);
						ShoppingCart gh = (ShoppingCart)sessionGH.getAttribute("gh");
						if(h.size() == 0) return;
						gh.updateQty(h.get(0), sl);
						sessionGH.setAttribute("gh", gh);
						response.sendRedirect("Trang_xem_gh.jsp");
					}
				}
				else{
					
					sessionHoamoi.setAttribute("listhoa", productDTO.ListProducts());	
					response.sendRedirect("Trang_chu.jsp");
					
				}
			}
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		HttpSession tb = request.getSession();
		HttpSession sessionGH = request.getSession();
		String action= request.getParameter("action");
		ShoppingCart gh = (ShoppingCart)sessionGH.getAttribute("gh");
		
		Products productDTO = new Products();
		List<ProductModel> shp = new ArrayList<ProductModel>();
		if(action.equals("mua")){
			String mahoa = request.getParameter("hoaid");
			shp = productDTO.getProducts(mahoa, null, null, null);
			if(shp.size() == 0) return;
			session.setAttribute("hoa", shp.get(0));
			response.sendRedirect("Trang_mua_hoa.jsp");
		}
		else{
			if(action.equals("dongymua")){
				String mahoa = request.getParameter("txtMahoa");
				String maloai = request.getParameter("txtMaloai");
				String tenhoa = request.getParameter("txtTenhoa");
				String gia = request.getParameter("txtGia");
				String soluong = request.getParameter("txtSoluong");
				
				int sl = Integer.parseInt(soluong);
				if(sl<1){
					String tbao = "Số lượng không hợp lý";
					tb.setAttribute("tb", tbao);
					response.sendRedirect("Trang_mua_hoa.jsp");
				}
				else{
				
				ProductModel p = new ProductModel();
				p.setId(mahoa);
				p.setType_id(maloai);
				p.setName(tenhoa);
				p.setPrice(Double.parseDouble(gia));
				p.setQuantities(Integer.parseInt(soluong));
				//GioHang gh = (GioHang)sessionGH.getAttribute("gh");
				
				
				if(gh != null){
					gh.add(p.getId(), p.getQuantities(), p);
				}
				
				else {
					gh = new ShoppingCart();
					gh.add(p.getId(), p.getQuantities(), p);
				}
				
				sessionGH.setAttribute("gh", gh);
				
				response.sendRedirect("Trang_chu.jsp");
				}
			}

		}
		if(action.equals("huygiohang")){
			sessionGH.removeAttribute("gh");
			out.println("vao phan huy");
			response.sendRedirect("Trang_xem_gh.jsp");
		}
		
	}
}
